import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../pages/Home.vue'
import ToolBox from '../pages/ToolBox.vue'
import { ToolType } from '@/types/tools'
import Store from '@/pages/Store.vue'
// 导入工具箱子路由数据
const components = import.meta.glob('@/pages/tools-page/**/*.vue')
import NoPage from '@/pages/tools-page/NoPage.vue'

const tools = await fetch('/data/tools.json').then((r) => r.json())
const toolRoutes = tools.map((t: ToolType) => ({
  path: t.routerPath,
  component: components[t.component],
}))

// 注册路由
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/toolbox',
      redirect: '/toolbox/noPage',
    },
    {
      path: '/home',
      component: Home,
    },
    {
      path: '/store',
      component: Store,
    },
    {
      path: '/toolbox',
      component: ToolBox,
      children: [
        {
          path: 'noPage',
          component: NoPage,
        },
        ...toolRoutes,
        {
          path: ':pathMatch(.*)*',
          redirect: '/toolbox/noPage',
        },
      ],
    },
  ],
})

export default router
